﻿package com.game.utils 
{
	
	
	import flash.display.MovieClip;
	import flash.events.Event;
	
	/**
	 * 
	 * @author zengly
	 */
	public class SetMcGoto 
	{
		private var _frameRate:uint=24;
		private var _mc:MovieClip;
		private var _minNum:Number;
		private var _maxNum:Number;
		private var _nowNum:Number;
		private var _currFrame:uint;
		private var _gotoFrame:uint;
		private var _type:int;
		private var _addFrameNum:Number;
		private var _addFrame:Number;
		private var _totalNum:Number;
		private var _totalFrames:uint;
		private var _recordFrame:uint;
		
		private var _isTrace:Boolean=false;

		private var _endFun:Function
		
		/**
		 * 让影片剪辑根据数值播放到制定帧
		 * @param type  type=0:无过程,type=1,有过程,type=2,按时间
		 * @param mc  设置影片剪辑
		 * @param nowNum 当前数值
		 * @param minNum 最小值
		 * @param maxNum 最大值
		 * @param endFun 播放到制定帧时调用
		 */
		public function SetMcGoto(type:int,mc:MovieClip,nowNum:Number=0,minNum:Number=0,maxNum:Number=0,endFun:Function=null)
		{
			_mc=mc ;
			_nowNum=nowNum;
			_minNum=minNum;
			_maxNum=maxNum;
			_type=type; 
			_totalNum=Math.abs(maxNum-minNum);
			_totalFrames=_mc.totalFrames;
			_endFun=endFun;
			_mc.gotoAndStop(getGotoFrame());
			_currFrame=_mc.currentFrame;
			
		}
		/**
		 * 修改当前数值
		 * @param nowNum 新数值
		 * @param time 改变到指定数值，花费的时间，单位秒
		 * 
		 */
		public function setNum(nowNum:Number,time:uint=0,currFrame:uint=0):void{
			_nowNum=nowNum;
			if(currFrame!=0){
				_mc.gotoAndStop(currFrame);
			}
			_gotoFrame=getGotoFrame();
			if(_type!=2){
				setGoto(_gotoFrame);
			}else{
				_addFrameNum=0
				_currFrame=_recordFrame=_mc.currentFrame;
				_addFrame=(_gotoFrame-_currFrame)/(_frameRate*time)//(Math.floor(_totalFrames*changNum/_totalNum)/_frameRate)/time;
				setTimeGoto();
			}
		}
		/**
		 * 修改帧频
		 * @param num 数值
		 * 
		 * */
		public function set rate(num:uint):void{
			_frameRate=num;
		}
		private function setTimeGoto():void{
			_mc.addEventListener(Event.ENTER_FRAME,onSetTimeGoto);
			
		}
		private function onSetTimeGoto(E:Event):void{
			if(_addFrame==0){
				return;
			}
			_addFrameNum+=_addFrame;

			var _tempFrame:Number=Math.floor(_recordFrame+_addFrameNum);
			if(_currFrame==_tempFrame){
				return;
			}
			_currFrame=_tempFrame;
				;
			_mc.gotoAndStop(_currFrame);
			if(_addFrame>0){
				if(_currFrame>=_gotoFrame){
					delTiemGoto();
				}
			}else{
				if(_currFrame<=_gotoFrame){
					delTiemGoto();
				}
			}
		}
		private function delTiemGoto():void{
			_mc.removeEventListener(Event.ENTER_FRAME,onSetTimeGoto);;
			_currFrame=_gotoFrame;
			_mc.gotoAndStop(_gotoFrame);
			if(_endFun!=null){
				_endFun();
			}
		}
		
		private function setGoto(fNum:uint):void{
			if(_type==0){
				_mc.gotoAndStop(fNum);
				this._currFrame=fNum;
			}else{
				_mc.addEventListener(Event.ENTER_FRAME,onEnterFrame);
			}
		}
		private function onEnterFrame(E:Event):void{
			
			
			if(_gotoFrame>_currFrame){
				_mc.gotoAndStop(_currFrame++);
				
			}else if(_gotoFrame<_currFrame){
				_mc.gotoAndStop(_currFrame--);
				
			}else{
				_mc.gotoAndStop(_gotoFrame);
				_currFrame=_gotoFrame;
				_mc.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
			}
			
			
			
		}
		private function getGotoFrame():uint{
			
			var _num:Number=_totalFrames*(_nowNum/_totalNum);
			var  thisGoto:uint;
			if(_maxNum<_minNum){
				thisGoto=_totalFrames-Math.ceil(_num);
			}else {
				thisGoto=Math.floor(_num);
			}
			
			
			if(thisGoto<=0){
				thisGoto=1;
			}
			
			return thisGoto;
		}
		public function get currFrame():uint
		{
			return _currFrame;
		}
		
		public function set currFrame(value:uint):void
		{
			_currFrame = value;
		}
		public function gc():void{
			_mc.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
			_mc.removeEventListener(Event.ENTER_FRAME,onSetTimeGoto);
		}
	}
}